package com.css.fxfzaqbz.modules.data.service.impl;

import com.css.fxfzaqbz.base.upload.entity.AttachmentInfoEntity;
import com.css.fxfzaqbz.base.upload.repository.AttachmentInfoRepository;
import com.css.fxfzaqbz.constants.YNEnum;
import com.css.fxfzaqbz.modules.data.entity.ReportTemplateEntity;
import com.css.fxfzaqbz.modules.data.queryParams.ReportTemplateEntityQueryParams;
import com.css.fxfzaqbz.modules.data.repository.ReportTemplateRepository;
import com.css.fxfzaqbz.modules.data.service.ReportTemplateService;
import com.css.fxfzaqbz.modules.data.vo.CheckReportVO;
import com.css.fxfzaqbz.modules.data.vo.ReportTemplateVO;
import com.css.fxfzaqbz.util.CreateGson;
import com.css.fxfzaqbz.util.PlatformDateUtils;
import com.css.fxfzaqbz.util.PlatformObjectUtils;
import com.css.fxfzaqbz.util.UUIDGenerator;
import com.css.fxfzaqbz.web.PlatformSessionContext;
import com.css.fxfzaqbz.zcpt.sys.entity.SUser;
import com.css.fxfzaqbz.zcpt.sys.service.SUserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.sql.Date;
import java.sql.Timestamp;
import java.util.List;
import java.util.Map;

/**
 * @author y1fan
 * @create 2022-11-04 15:25
 */
@Service
public class ReportTemplateServiceImpl implements ReportTemplateService {


    @Resource
    private ReportTemplateRepository reportTemplateRepository;

    @Resource
    private SUserService sUserService;

    @Override
    @Transactional
    public void saveReportTemplate(List<ReportTemplateVO> reportTemplateVOS) {

        for (ReportTemplateVO reportTemplateVO : reportTemplateVOS) {
            if (!PlatformObjectUtils.isEmpty(reportTemplateVO.getName())){
                ReportTemplateEntity reportTemplateEntity=new ReportTemplateEntity();
                reportTemplateEntity.setRtId(UUIDGenerator.getUUID());
                //当前登录人ID
                String userID = PlatformSessionContext.getUserID();
                reportTemplateEntity.setRtCreateUser(userID);
                //获取当前登录人名字
                String userJson = sUserService.getSUser(userID);
                SUser sUser = CreateGson.createGson().fromJson(userJson, SUser.class);
                if (sUser != null) {
                    reportTemplateEntity.setRtPepole(sUser.getUserName());
                }
                //上传时间
                Timestamp currentTimestamp = PlatformDateUtils.getCurrentTimestamp();
                reportTemplateEntity.setRtTime(currentTimestamp);
                reportTemplateEntity.setRtCreateTime(currentTimestamp);
                reportTemplateEntity.setDelFlag(YNEnum.N.toString());
                reportTemplateEntity.setRtType(reportTemplateVO.getType());
                reportTemplateEntity.setRtName(reportTemplateVO.getName());
                reportTemplateEntity.setAttId(reportTemplateVO.getId());
                reportTemplateRepository.saveReportTemplate(reportTemplateEntity);
            }
        }

    }

    @Override
    public Map<String, Object> getAllReportTemplate(ReportTemplateEntityQueryParams reportTemplateEntityQueryParams) {
        return reportTemplateRepository.getAllReportTemplate(reportTemplateEntityQueryParams);
    }

    @Override
    @Transactional
    public void delReportTemplate(List<String> ids) {
        String userID = PlatformSessionContext.getUserID();
        Timestamp timestamp = PlatformDateUtils.getCurrentTimestamp();
        reportTemplateRepository.delReportTemplate(ids,userID,timestamp);
    }

    @Override
    public List<ReportTemplateEntity> getList(String rtType) {
        return reportTemplateRepository.getList(rtType);
    }

    @Override
    public Boolean checkReportTemplate(CheckReportVO checkReportVO) {
        return reportTemplateRepository.checkReportTemplate(checkReportVO);
    }
}
